﻿using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional

namespace WeatherSystem.API.Migrations
{
    /// <inheritdoc />
    public partial class EAVModelMigration : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_WeatherData_Cities_CityId",
                table: "WeatherData");

            migrationBuilder.DropPrimaryKey(
                name: "PK_WeatherData",
                table: "WeatherData");

            migrationBuilder.RenameTable(
                name: "WeatherData",
                newName: "WeatherData_Legacy");

            migrationBuilder.AddPrimaryKey(
                name: "PK_WeatherData_Legacy",
                table: "WeatherData_Legacy",
                column: "Id");

            migrationBuilder.CreateTable(
                name: "MetricDefinitions",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "TEXT", nullable: false),
                    Key = table.Column<string>(type: "TEXT", maxLength: 100, nullable: false),
                    Name = table.Column<string>(type: "TEXT", maxLength: 200, nullable: false),
                    Unit = table.Column<string>(type: "TEXT", maxLength: 20, nullable: false),
                    Description = table.Column<string>(type: "TEXT", maxLength: 500, nullable: true),
                    DataType = table.Column<string>(type: "TEXT", maxLength: 20, nullable: false),
                    IsRequired = table.Column<bool>(type: "INTEGER", nullable: false),
                    IsDefault = table.Column<bool>(type: "INTEGER", nullable: false),
                    IsActive = table.Column<bool>(type: "INTEGER", nullable: false),
                    DisplayOrder = table.Column<int>(type: "INTEGER", nullable: false),
                    ValidationRules = table.Column<string>(type: "TEXT", nullable: true),
                    CreatedAt = table.Column<DateTime>(type: "TEXT", nullable: false),
                    UpdatedAt = table.Column<DateTime>(type: "TEXT", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_MetricDefinitions", x => x.Id);
                    table.UniqueConstraint("AK_MetricDefinitions_Key", x => x.Key);
                });

            migrationBuilder.CreateTable(
                name: "WeatherDataEntries",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "TEXT", nullable: false),
                    CityId = table.Column<Guid>(type: "TEXT", nullable: false),
                    RecordedAt = table.Column<DateTime>(type: "TEXT", nullable: false),
                    Source = table.Column<string>(type: "TEXT", maxLength: 50, nullable: true),
                    CreatedAt = table.Column<DateTime>(type: "TEXT", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_WeatherDataEntries", x => x.Id);
                    table.ForeignKey(
                        name: "FK_WeatherDataEntries_Cities_CityId",
                        column: x => x.CityId,
                        principalTable: "Cities",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Restrict);
                });

            migrationBuilder.CreateTable(
                name: "MetricValues",
                columns: table => new
                {
                    Id = table.Column<Guid>(type: "TEXT", nullable: false),
                    WeatherDataEntryId = table.Column<Guid>(type: "TEXT", nullable: false),
                    MetricKey = table.Column<string>(type: "TEXT", maxLength: 100, nullable: false),
                    RawValue = table.Column<string>(type: "TEXT", maxLength: 500, nullable: false),
                    DataType = table.Column<string>(type: "TEXT", maxLength: 20, nullable: false),
                    CreatedAt = table.Column<DateTime>(type: "TEXT", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_MetricValues", x => x.Id);
                    table.ForeignKey(
                        name: "FK_MetricValues_MetricDefinitions_MetricKey",
                        column: x => x.MetricKey,
                        principalTable: "MetricDefinitions",
                        principalColumn: "Key",
                        onDelete: ReferentialAction.Restrict);
                    table.ForeignKey(
                        name: "FK_MetricValues_WeatherDataEntries_WeatherDataEntryId",
                        column: x => x.WeatherDataEntryId,
                        principalTable: "WeatherDataEntries",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.InsertData(
                table: "MetricDefinitions",
                columns: new[] { "Id", "CreatedAt", "DataType", "Description", "DisplayOrder", "IsActive", "IsDefault", "IsRequired", "Key", "Name", "Unit", "UpdatedAt", "ValidationRules" },
                values: new object[,]
                {
                    { new Guid("aaaaaaaa-1111-1111-1111-111111111111"), new DateTime(2024, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), "decimal", "环境温度", 1, true, true, true, "temperature", "温度", "°C", new DateTime(2024, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), "{\"MinValue\": -50, \"MaxValue\": 60}" },
                    { new Guid("aaaaaaaa-2222-2222-2222-222222222222"), new DateTime(2024, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), "decimal", "相对湿度", 2, true, true, true, "humidity", "湿度", "%", new DateTime(2024, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), "{\"MinValue\": 0, \"MaxValue\": 100}" },
                    { new Guid("aaaaaaaa-3333-3333-3333-333333333333"), new DateTime(2024, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), "decimal", "大气压强", 3, true, true, true, "pressure", "气压", "hPa", new DateTime(2024, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), "{\"MinValue\": 900, \"MaxValue\": 1100}" },
                    { new Guid("aaaaaaaa-4444-4444-4444-444444444444"), new DateTime(2024, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), "decimal", "风速", 4, true, true, true, "windSpeed", "风速", "m/s", new DateTime(2024, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), "{\"MinValue\": 0, \"MaxValue\": 100}" },
                    { new Guid("aaaaaaaa-5555-5555-5555-555555555555"), new DateTime(2024, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), "decimal", "风向角度", 5, true, false, false, "windDirection", "风向", "°", new DateTime(2024, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), "{\"MinValue\": 0, \"MaxValue\": 360}" },
                    { new Guid("aaaaaaaa-6666-6666-6666-666666666666"), new DateTime(2024, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), "decimal", "大气能见度", 6, true, false, false, "visibility", "能见度", "km", new DateTime(2024, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), "{\"MinValue\": 0, \"MaxValue\": 50}" },
                    { new Guid("aaaaaaaa-7777-7777-7777-777777777777"), new DateTime(2024, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), "decimal", "紫外线强度指数", 7, true, false, false, "uvIndex", "紫外线指数", "", new DateTime(2024, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), "{\"MinValue\": 0, \"MaxValue\": 15}" },
                    { new Guid("aaaaaaaa-8888-8888-8888-888888888888"), new DateTime(2024, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), "string", "天气描述", 8, true, true, false, "weatherCondition", "天气状况", "", new DateTime(2024, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc), "{\"MaxLength\": 100}" }
                });

            migrationBuilder.UpdateData(
                table: "Users",
                keyColumn: "Id",
                keyValue: new Guid("77777777-7777-7777-7777-777777777777"),
                column: "UpdatedAt",
                value: new DateTime(2025, 9, 1, 11, 59, 45, 381, DateTimeKind.Utc).AddTicks(9420));

            migrationBuilder.CreateIndex(
                name: "IX_MetricDefinition_IsActive_DisplayOrder",
                table: "MetricDefinitions",
                columns: new[] { "IsActive", "DisplayOrder" });

            migrationBuilder.CreateIndex(
                name: "IX_MetricDefinition_Key",
                table: "MetricDefinitions",
                column: "Key",
                unique: true);

            migrationBuilder.CreateIndex(
                name: "IX_MetricValue_EntryId_MetricKey",
                table: "MetricValues",
                columns: new[] { "WeatherDataEntryId", "MetricKey" });

            migrationBuilder.CreateIndex(
                name: "IX_MetricValue_MetricKey_RawValue",
                table: "MetricValues",
                columns: new[] { "MetricKey", "RawValue" });

            migrationBuilder.CreateIndex(
                name: "IX_WeatherDataEntry_CityId_RecordedAt",
                table: "WeatherDataEntries",
                columns: new[] { "CityId", "RecordedAt" });

            migrationBuilder.CreateIndex(
                name: "IX_WeatherDataEntry_RecordedAt",
                table: "WeatherDataEntries",
                column: "RecordedAt");

            migrationBuilder.AddForeignKey(
                name: "FK_WeatherData_Legacy_Cities_CityId",
                table: "WeatherData_Legacy",
                column: "CityId",
                principalTable: "Cities",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropForeignKey(
                name: "FK_WeatherData_Legacy_Cities_CityId",
                table: "WeatherData_Legacy");

            migrationBuilder.DropTable(
                name: "MetricValues");

            migrationBuilder.DropTable(
                name: "MetricDefinitions");

            migrationBuilder.DropTable(
                name: "WeatherDataEntries");

            migrationBuilder.DropPrimaryKey(
                name: "PK_WeatherData_Legacy",
                table: "WeatherData_Legacy");

            migrationBuilder.RenameTable(
                name: "WeatherData_Legacy",
                newName: "WeatherData");

            migrationBuilder.AddPrimaryKey(
                name: "PK_WeatherData",
                table: "WeatherData",
                column: "Id");

            migrationBuilder.UpdateData(
                table: "Users",
                keyColumn: "Id",
                keyValue: new Guid("77777777-7777-7777-7777-777777777777"),
                column: "UpdatedAt",
                value: new DateTime(2025, 9, 1, 9, 37, 9, 944, DateTimeKind.Utc).AddTicks(7652));

            migrationBuilder.AddForeignKey(
                name: "FK_WeatherData_Cities_CityId",
                table: "WeatherData",
                column: "CityId",
                principalTable: "Cities",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);
        }
    }
}
